渲染zh-Hant

求和

Translation status: 繁體中文 reader-locale proof. Term names and code fences follow the zh-Hant pack; supporting prose may still be English.

Sequential sum-reduce expression: 求和 取自 <tensor> 在 [i] 定值 s { 傳回 <term> }.

Syntax: 求和 取自 <source> 在 [coords] 定值 <binder> { 傳回 <term> }

Category#

collection

Examples#

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

Returns the sum of numeric list elements.

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

# 列表<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' | '歸約' | 'cumulata') '(' closure ')'
#
# EXPECTED OUTPUT:
#   [2, 4]
#   [2, 4, 6, 8, 10]
#   15
#   [1, 3, 6, 10, 15]
#
# BACKEND: Go e2e whitelist — 列表 intrinsic methods not yet lowered for Go
# (whitelist: 列表/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 求和 ← 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 求和
    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: 求和 取自 <tensor> 在 [i] 定值 s { 傳回 <term> }.

# =============================================================================
# 求和 — one-expression sequential sum-reduce over a 張量
# =============================================================================
#
# What this teaches:
#   • Sequential fold — `求和 取自` lowers to a MIR fold over the 張量's
#     在 iteration arm: a `+` accumulator seeded at zero, one term per
#     element, `傳回` inside the body yields the term value.
#   • Identity term — `傳回 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 車道).
#
# Common mistakes:
#   • non-scalar terms — the term must be a numeric scalar (小數/numerus)
#   • the distributed `執行緒` spelling is admitted only inside `@ 內核`
#     kernels (v1)
#
# See also: 張量, 在, 執行緒
# =============================================================================

函式 identity(張量<f32, [8]> a) → f32 {
    傳回 求和 取自 a 在 [i] 定值 s {
        傳回 s
    }
}

函式 shifted(張量<f32, [8]> a) → f32 {
    傳回 求和 取自 a 在 [i] 定值 s {
        變值 f32 t ← s
        t ← t + 1.0
        傳回 t
    }
}

入口 {
    定值 列表<f32> flat ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
    定值 tf32[] seed ← 空集
    定值 tf32[8] a ← seed.由扁平建構(flat, [8])
    定值 f32 per_summam ← a.求和()
    定值 f32 plicatum ← identity(a)
    定值 f32 summam_totam ← shifted(a)
    註記 per_summam
    註記 plicatum
    註記 summam_totam
}

Expected output:

36.0
36.0
44.0

radix/corpus/summa/summa-combine-refusal.fab (supporting · reject)#

General-combine refusal: 求和 admits no identity/combine clause — the combine is sum only, pinned by a parse reject.

# =============================================================================
# 求和 general-combine refusal — no identity/combine clause exists.
# =============================================================================
#
# Decline row (reject reason in the comment):
#   1. general combine — the 歸約 ruling bars overloading `求和` 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 `求和` to spell product/min/max reduces — the combine is
#     sum only; general reduce/scan heads belong to the rejection-review
#     sketch, and `歸約`/`cumulata` own the intrinsic family
#
# See also: 求和, 執行緒, 在, 張量
# =============================================================================

# 1. general combine — a combine-operator tail is not grammar; parse reject
函式 product(張量<f32, [8]> a) → f32 {
    傳回 求和 取自 a 在 [i] 定值 sconiunge '*' { 傳回 s }
}

Expected: compilation rejects this example.

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

求和 decline rows: 執行緒 placement outside @ 內核, K % 32 ≠ 0 tails, and non-scalar terms fail closed.

# =============================================================================
# 求和 decline — placement, tail alignment, and scalar terms fail closed.
# =============================================================================
#
# Decline rows (reject reason in each comment):
#   1. placement — `執行緒` admits only inside an `@ 內核` 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 矩陣 row (a 張量),
#      not a numeric scalar: SEM010 summa_term_type_non_scalar
#
# Common mistakes:
#   • expecting a partial-lane 執行緒 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: 求和, 執行緒, 內核, 在, 張量
# =============================================================================

# 1. placement — 執行緒 outside @ 內核 declines
函式 placement(張量<f32, [32]> a) → f32 {
    傳回 求和 取自 a 在 [i] 執行緒 f 定值 s { 傳回 s }
}

# 2. tail — K = 8 not aligned to the 32-lane width declines
@ 內核
@ 公開
函式 tail(張量<f32, [8]> a, tf32[1] out) → 空值 {
    定值 f32 total ← 求和 取自 a 在 [i] 執行緒 f 定值 s { 傳回 s }
    out[0] ← total
}

# 3. non-scalar term — a 文字 element is not a numeric scalar
函式 non_scalar(張量<文字, [8]> t) → 文字 {
    傳回 求和 取自 t 在 [i] 定值 s { 傳回 s }
}

Expected: compilation rejects this example.