tổng
Translation status: Tiếng Việt reader-locale proof. Term names and code fences follow the vi pack; supporting prose may still be English.
Sequential sum-reduce expression: tổng từ <tensor> tại [i] hằng s { trả <term> }.
Syntax: tổng từ <source> tại [coords] hằng <binder> { trả <term> }
Category#
collection
Related#
- prima
- ultima
- danh_sách
Examples#
radix/corpus/lista/methodi-functionales.fab (canonical · existing-home)#
Returns the sum of numeric list elements.
# =============================================================================
# tổng — Returns the sum of numeric list elements.
# =============================================================================
#
# What this teaches:
# • higher-order intrinsics for lists — `filtrata` (filter), `mappata` (map), `rút_gọn` (fold), `cumulata` (cumulate)
# • closure syntax with `∴` — inline anonymous functions for collection operations
#
# Common mistakes:
# • Omitting the initial accumulator value in rút_gọn/cumulata — both require an explicit seed argument.
#
# See also: prima, ultima, danh_sách
# =============================================================================
# danh_sách<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' | 'rút_gọn' | 'cumulata') '(' closure ')'
#
# EXPECTED OUTPUT:
# [2, 4]
# [2, 4, 6, 8, 10]
# 15
# [1, 3, 6, 10, 15]
#
# BACKEND: Go e2e whitelist — danh_sách intrinsic methods not yet lowered for Go
# (whitelist: danh_sách/methodi-functionales.fab).
main {
const list<int> res ← [1, 2, 3, 4, 5] ∷ list<int>
const list<int> pares ← res.filter(int x ∴ x % 2 ≡ 0)
const list<int> duplicata ← res.map(int x ∴ x * 2)
const int tổng ← res.reduce((int collectus, int x) ∴ collectus + x, 0)
const list<int> cumulata ← res.cumulate((int collectus, int x) ∴ collectus + x, 0)
print pares
print duplicata
print tổng
print cumulata
}Expected output:
[2, 4]
[2, 4, 6, 8, 10]
15
[1, 3, 6, 10, 15]
radix/corpus/summa/summa.fab (canonical · concept)#
Sequential sum-reduce expression: tổng từ <tensor> tại [i] hằng s { trả <term> }.
# =============================================================================
# tổng — one-expression sequential sum-reduce over a ten_xo
# =============================================================================
#
# What this teaches:
# • Sequential fold — `tổng từ` lowers to a MIR fold over the ten_xo's
# tại iteration arm: a `+` accumulator seeded at zero, one term per
# element, `trả` inside the body yields the term value.
# • Identity term — `trả 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 làn).
#
# Common mistakes:
# • non-scalar terms — the term must be a numeric scalar (thập_phân/numerus)
# • the distributed `sợi` spelling is admitted only inside `@ hạt_nhân`
# kernels (v1)
#
# See also: ten_xo, tại, sợi
# =============================================================================
hàm identity(ten_xo<f32, [8]> a) → f32 {
trả tổng từ a tại [i] hằng s {
trả s
}
}
hàm shifted(ten_xo<f32, [8]> a) → f32 {
trả tổng từ a tại [i] hằng s {
biến f32 t ← s
t ← t + 1.0
trả t
}
}
bắt_đầu {
hằng danh_sách<f32> flat ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
hằng tf32[] seed ← tập_rỗng
hằng tf32[8] a ← seed.dựng_từ_phẳng(flat, [8])
hằng f32 per_summam ← a.tổng()
hằng f32 plicatum ← identity(a)
hằng f32 summam_totam ← shifted(a)
ghi_chú per_summam
ghi_chú plicatum
ghi_chú summam_totam
}Expected output:
36.0
36.0
44.0
radix/corpus/summa/summa-combine-refusal.fab (supporting · reject)#
General-combine refusal: tổng admits no identity/combine clause — the combine is sum only, pinned by a parse reject.
# =============================================================================
# tổng general-combine refusal — no identity/combine clause exists.
# =============================================================================
#
# Decline row (reject reason in the comment):
# 1. general combine — the rút_gọn ruling bars overloading `tổng` with a
# general combiner; there is no identity or combine clause in the
# grammar. An attempted `coniunge` tail after the binder fails at
# parse: the production expects the term block, never an operator
#
# Common mistakes:
# • expecting `tổng` to spell product/min/max reduces — the combine is
# sum only; general reduce/scan heads belong to the rejection-review
# sketch, and `rút_gọn`/`cumulata` own the intrinsic family
#
# See also: tổng, sợi, tại, ten_xo
# =============================================================================
# 1. general combine — a combine-operator tail is not grammar; parse reject
hàm product(ten_xo<f32, [8]> a) → f32 {
trả tổng từ a tại [i] hằng sconiunge '*' { trả s }
}Expected: compilation rejects this example.
radix/corpus/summa/summa-decline.fab (supporting · reject)#
tổng decline rows: sợi placement outside @ hạt_nhân, K % 32 ≠ 0 tails, and non-scalar terms fail closed.
# =============================================================================
# tổng decline — placement, tail alignment, and scalar terms fail closed.
# =============================================================================
#
# Decline rows (reject reason in each comment):
# 1. placement — `sợi` admits only inside an `@ hạt_nhân` kernel;
# this host function declines: SEM010 summa_filum_requires_nucleum_kernel
# 2. tail — K = 8 is not a multiple of the 32-lane width; the v1 tail
# gate fails closed at admission:
# SEM010 summa_tail_not_aligned
# 3. non-scalar term — the term body returns a ma_trận row (a ten_xo),
# not a numeric scalar: SEM010 summa_term_type_non_scalar
#
# Common mistakes:
# • expecting a partial-lane sợi fold — K % W ≠ 0 is an admission
# error, never a tail loop
# • expecting list/tensor terms to combine element-wise — the term type
# must be a scalar of the element family
#
# See also: tổng, sợi, hạt_nhân, tại, ten_xo
# =============================================================================
# 1. placement — sợi outside @ hạt_nhân declines
hàm placement(ten_xo<f32, [32]> a) → f32 {
trả tổng từ a tại [i] sợi f hằng s { trả s }
}
# 2. tail — K = 8 not aligned to the 32-lane width declines
@ hạt_nhân
@ công_khai
hàm tail(ten_xo<f32, [8]> a, tf32[1] out) → trống {
hằng f32 total ← tổng từ a tại [i] sợi f hằng s { trả s }
out[0] ← total
}
# 3. non-scalar term — a văn_bản element is not a numeric scalar
hàm non_scalar(ten_xo<văn_bản, [8]> t) → văn_bản {
trả tổng từ t tại [i] hằng s { trả s }
}Expected: compilation rejects this example.