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.

tensor

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

tensor<T, Figura> declaration shell with rank-0 empty.

Syntax: tensor<T, Figura> | tensor<T, []>

Category#

tensor

Examples#

radix/corpus/reference/operator-group/tensor/equality-whole.fab (canonical · operator-group)#

≡ and ≢ compare whole tensors and give one yes/no; ≅ and ≇ give a per-element mask, and a mask reduces to one answer.

# =============================================================================
# tensor — Whole-value equality
# =============================================================================
#
# What this teaches:
#   • ≡ and ≢ compare WHOLE tensors — the same shape and every element pair equal — and give one bool
#   • ≅ and ≇ compare per element and give a bool tensor of the same shape (a mask)
#   • A mask is never a condition — reduce it with `reduce via all` or `reduce via any`
#   • NaN is not equal to anything, so a tensor holding one is not equal to itself
#   • A shape known only at run time makes ≡ false, never an error
#
# Common mistakes:
#   • writing `a ≡ 2.0` for a mask — a tensor is never equal to a number; write `a ≅ 2.0`
#   • using a mask as an `if` condition — reduce it first
#   • comparing tensors whose shapes are written differently in their types — that is a compile error
#
# See also: tensor, reduce, bool
# =============================================================================

# tensor — ≡ is whole-value, ≅ is per element
#
# WHY: ≡ and ≢ mean the same on every type: are these the same value? A kernel's
# equality mask is `a ≅ b`, its ordering mask is `a > b`.
#
# EXPECTED stdout: see equality-whole.expected

fn runtime_extent(int value) → int {
    return value
}

main {
    let tensor<f32, [4]> a ← [1.0, 2.0, 3.0, 4.0] ↦ tensor<f32, [4]>
    let tensor<f32, [4]> same ← [1.0, 2.0, 3.0, 4.0] ↦ tensor<f32, [4]>
    let tensor<f32, [4]> other ← [1.0, 2.0, 3.0, 5.0] ↦ tensor<f32, [4]>
    print a ≡ same
    print a ≡ other
    print a ≢ other
    print a ≢ same
    let tensor<bool, [4]> mask ← a ≅ other
    print mask.flatten()
    let tensor<bool, [4]> apart ← a ≇ other
    print apart.flatten()
    print reduce via all from mask
    print reduce via any from apart
    print reduce via count from mask
    let tensor<f32, [4]> zeros ← [0.0, 1.0, 2.0, 3.0] ↦ tensor<f32, [4]>
    let tensor<f32, [4]> nans ← zeros ÷ zeros
    print nans.flatten()
    print nans ≡ nans
    print nans ≢ nans
    let int n ← runtime_extent(2)
    let int m ← runtime_extent(3)
    let list<f32> flat_short ← [1.0, 2.0]
    let list<f32> flat_long ← [1.0, 2.0, 3.0]
    let tensor<f32, [size n]> short ← flat_short ↦ tensor<f32, [size n]>
    let tensor<f32, [size m]> long ← flat_long ↦ tensor<f32, [size m]>
    print short ≡ long
    print short ≢ long
    print short ≡ short
}

Expected output:

true
false
true
false
[true, true, true, false]
[false, false, false, true]
false
true
3
[NaN, 1.0, 1.0, 1.0]
false
true
false
true
true

radix/corpus/reference/operator-group/tensor/lift-arith.fab (canonical · operator-group)#

Tensor arithmetic lifts over a scalar: + - * / % ÷ and unary minus apply per element; ⤒ ⤓ take the elementwise extremum of two tensors.

# =============================================================================
# tensor — Elementwise arithmetic lifted over a scalar.
# =============================================================================
#
# What this teaches:
#   • Lifting — a scalar operator applied per element: `a + 1.0`, `10.0 - a`, `a * 0.5`
#   • `* / %` lift only against a scalar; a tensor times a tensor is `⊙` (elementwise) or `·` (contraction)
#   • Division has two kinds — `/` floors on integers, `÷` is true division (16-bit and narrower integers give f32)
#   • Integer results follow the scalar range rule and width applies at the store: `/` on `i16` is typed `i32` (the bound is [-M, M]), so storing it back into an `i16` tensor narrows at the store like a scalar, checked implicitly; the explicit `↦` stays optional; `%` by a positive constant is non-negative and fits a wider tensor directly
#   • Extrema — `a ⤒ b` and `a ⤓ b` pick the larger or smaller element of two same-shape tensors
#   • Widths follow the scalar rules: an untyped constant takes part by value, a family crossing needs `↦`
#
# Common mistakes:
#   • writing `a * b` for an elementwise product — use `a ⊙ b`
#   • mixing a float scalar with an integer tensor (`n * 0.5`) — convert the tensor with `↦` first
#   • expecting tensors of different shapes to stretch — shapes must be identical
#
# See also: tensor, ⊙, ÷
# =============================================================================

# tensor lift — arithmetic against a scalar
#
# WHY: each operator is the scalar operator applied to every element, so the
# runner's answer for one element is the answer for the whole tensor. An integer
# result is typed by the scalar range rule and is stored per element into the
# declared tensor; a result wider than the declared element narrows at the store,
# checked implicitly like a scalar (an explicit `↦` stays optional).
#
# EXPECTED stdout:
#   [2.0, 3.0, 4.0, 5.0]
#   [9.0, 8.0, 7.0, 6.0]
#   [0.5, 1.0, 1.5, 2.0]
#   [0.25, 0.5, 0.75, 1.0]
#   [1.0, 2.0, 0.0, 1.0]
#   [-1.0, -2.0, -3.0, -4.0]
#   [0, 1, 1, 2]
#   [0, 1, 1, 2]
#   [1, 0, 1, 0]
#   [0.5, 1.0, 1.5, 2.0]
#   [4.0, 3.0, 3.0, 4.0]
#   [1.0, 2.0, 2.0, 1.0]

main {
    let list<f32> flat_a ← [1.0, 2.0, 3.0, 4.0]
    let list<f32> flat_b ← [4.0, 3.0, 2.0, 1.0]
    mut tensor<f32, [2, 2]> a ← flat_a ↦ tensor<f32, [2, 2]>
    mut tensor<f32, [2, 2]> b ← flat_b ↦ tensor<f32, [2, 2]>
    let tensor<f32, [2, 2]> plus ← a + 1.0
    let list<f32> plus_out ← plus.flatten()
    print plus_out
    let tensor<f32, [2, 2]> minus ← 10.0 - a
    let list<f32> minus_out ← minus.flatten()
    print minus_out
    let tensor<f32, [2, 2]> times ← a * 0.5
    let list<f32> times_out ← times.flatten()
    print times_out
    let tensor<f32, [2, 2]> divided ← a / 4.0
    let list<f32> divided_out ← divided.flatten()
    print divided_out
    let tensor<f32, [2, 2]> rest ← a % 3.0
    let list<f32> rest_out ← rest.flatten()
    print rest_out
    let tensor<f32, [2, 2]> negated ← -a
    let list<f32> negated_out ← negated.flatten()
    print negated_out
    let list<i16> flat_n ← [1, 2, 3, 4]
    mut tensor<i16, [4]> n ← flat_n ↦ tensor<i16, [4]>
    let tensor<i16, [4]> floored ← n / 2 ↦ tensor<i16, [4]>
    let list<i16> floored_out ← floored.flatten()
    print floored_out
    let tensor<i16, [4]> floored_implicit ← n / 2
    let list<i16> floored_implicit_out ← floored_implicit.flatten()
    print floored_implicit_out
    let tensor<i16, [4]> odd ← n % 2
    let list<i16> odd_out ← odd.flatten()
    print odd_out
    let tensor<f32, [4]> true_half ← n ÷ 2
    let list<f32> true_half_out ← true_half.flatten()
    print true_half_out
    let tensor<f32, [2, 2]> larger ← a.maximum(b)
    let list<f32> larger_out ← larger.flatten()
    print larger_out
    let tensor<f32, [2, 2]> smaller ← a.minimum(b)
    let list<f32> smaller_out ← smaller.flatten()
    print smaller_out
}

Expected output:

[2.0, 3.0, 4.0, 5.0]
[9.0, 8.0, 7.0, 6.0]
[0.5, 1.0, 1.5, 2.0]
[0.25, 0.5, 0.75, 1.0]
[1.0, 2.0, 0.0, 1.0]
[-1.0, -2.0, -3.0, -4.0]
[0, 1, 1, 2]
[0, 1, 1, 2]
[1, 0, 1, 0]
[0.5, 1.0, 1.5, 2.0]
[4.0, 3.0, 3.0, 4.0]
[1.0, 2.0, 2.0, 1.0]

radix/corpus/reference/operator-group/tensor/lift-compare.fab (canonical · operator-group)#

Tensor comparisons and logic lift per element: comparisons give a bool tensor, and/or/not and the ✓ ✗ select evaluate both sides, and vel fills the none elements.

# =============================================================================
# tensor — Elementwise comparison, logic, select and vel.
# =============================================================================
#
# What this teaches:
#   • Comparisons — `< > ≤ ≥ ≅ ≇` on tensors give `tensor<bool, S>`, one verdict per element
#   • Whole values — `≡` and `≢` compare the whole tensor and give one bool (see equality-whole)
#   • Logic — `and`, `or`, `not` act per element on bool tensors and always evaluate both sides
#   • Select — `mask ✓ a ✗ b` takes `a` where the mask is true and `b` elsewhere; both branches are evaluated
#   • vel (spelled `coalesce` in English) — `t coalesce fallback` fills the `none` elements of a `T ∪ none` tensor
#   • NaN compares false to everything, including itself
#
# Common mistakes:
#   • using a tensor mask as an `if` condition — a mask is a tensor; select or reduce it
#   • expecting `and` to skip its right side on a mask — both sides run
#   • using `coalesce` on a tensor whose elements can never be none
#
# See also: tensor, vel, ✓ ✗, equality-whole
# =============================================================================

# tensor lift — comparisons, logic, select, vel
#
# WHY: a comparison is the scalar comparison per element, and its result is a
# bool tensor of the same shape. Logic and select take such masks.
#
# EXPECTED stdout:
#   [false, false, true, true]
#   [true, true, false, false]
#   [false, true, false, false]
#   [true, true, true, true]
#   [false, false, true, true]
#   [true, false, false, true]
#   [false, true, false, false]
#   [4.0, 3.0, 3.0, 4.0]
#   [0.0, 0.0, 1.0, 1.0]
#   [1, 0, 3, 0]
#   [1, 20, 3, 40]

main {
    let list<f32> flat_a ← [1.0, 2.0, 3.0, 4.0]
    let list<f32> flat_b ← [4.0, 3.0, 2.0, 1.0]
    mut tensor<f32, [2, 2]> a ← flat_a ↦ tensor<f32, [2, 2]>
    mut tensor<f32, [2, 2]> b ← flat_b ↦ tensor<f32, [2, 2]>
    let tensor<bool, [2, 2]> greater ← a ≻ b
    let list<bool> greater_out ← greater.flatten()
    print greater_out
    let tensor<bool, [2, 2]> small ← a ≤ 2.0
    let list<bool> small_out ← small.flatten()
    print small_out
    let tensor<bool, [2, 2]> level ← a ≅ 2.0
    let list<bool> level_out ← level.flatten()
    print level_out
    let tensor<bool, [2, 2]> unlike ← a ≇ b
    let list<bool> unlike_out ← unlike.flatten()
    print unlike_out
    let tensor<bool, [2, 2]> not_small ← not small
    let list<bool> not_small_out ← not_small.flatten()
    print not_small_out
    let tensor<bool, [2, 2]> outer ← a ≺ 2.0 or a ≻ 3.0
    let list<bool> outer_out ← outer.flatten()
    print outer_out
    let tensor<bool, [2, 2]> inner ← a ≻ 1.0 and small
    let list<bool> inner_out ← inner.flatten()
    print inner_out
    let tensor<f32, [2, 2]> bigger ← greater ✓ a ✗ b
    let list<f32> bigger_out ← bigger.flatten()
    print bigger_out
    let tensor<f32, [2, 2]> clipped ← a ≻ 2.5 ✓ 1.0 ✗ 0.0
    let list<f32> clipped_out ← clipped.flatten()
    print clipped_out
    let list<int ∪ none> flat_h ← [1, null, 3, null]
    mut tensor<int ∪ none, [2, 2]> holes ← flat_h ↦ tensor<int ∪ none, [2, 2]>
    let tensor<int, [2, 2]> zeroed ← holes coalesce 0
    let list<int> zeroed_out ← zeroed.flatten()
    print zeroed_out
    let list<int> flat_d ← [10, 20, 30, 40]
    mut tensor<int, [2, 2]> defaults ← flat_d ↦ tensor<int, [2, 2]>
    let tensor<int, [2, 2]> filled ← holes coalesce defaults
    let list<int> filled_out ← filled.flatten()
    print filled_out
}

Expected output:

[false, false, true, true]
[true, true, false, false]
[false, true, false, false]
[true, true, true, true]
[false, false, true, true]
[true, false, false, true]
[false, true, false, false]
[4.0, 3.0, 3.0, 4.0]
[0.0, 0.0, 1.0, 1.0]
[1, 0, 3, 0]
[1, 20, 3, 40]

radix/corpus/reference/type/tensor/decl.fab (canonical · type)#

tensor<T, Figura> declaration shell with rank-0 empty.

# =============================================================================
# tensor — tensor<T, Figura> declaration shell with rank-0 empty.
# =============================================================================
#
# What this teaches:
#   • Tensor declaration — `tensor<T, Figura>` type syntax with `empty` as the empty initializer
#   • Identity functions — passing tensors through functions preserves shape and rank
#
# Common mistakes:
#   • declaring tensor<T> without a Figura shape parameter — use tensor<T, Figura> with explicit dimensions
#
# See also: tensor, list
# =============================================================================

# tensor — declaration shell for tensor<T, Figura>
#
# tensor<T, []> name ← empty
# fn f(tensor<T, []> value) → tensor<T, []>
#
# GRAMMAR:
#   type :← 'tensor' '<' type ',' shape '>'
#
# EXPECTED OUTPUT:
#   Rank-0 tensor longitudo after round-trip identity call.

fn identity(tensor<f32, []> value) → tensor<f32, []> {
    return value
}

main {
    let tensor<f32, []> empty ← empty
    let tensor<f32, []> roundtrip ← identity(empty)
    print roundtrip.length()
}

Expected output:

1

radix/corpus/reference/reject/tensor/arithmetic-reject.fab (supporting · reject)#

Tensor arithmetic rejects non-numeric elements and mixed numeric widths.

# =============================================================================
# tensor — Tensor arithmetic rejects non-numeric elements and mixed numeric widths.
# =============================================================================
#
# What this teaches:
#   • Type guard — elementwise arithmetic rejects non-numeric element types at compile time
#   • Width safety — mixed-width operations (e.g. i32 + i64) require explicit `↦` conversion
#
# Common mistakes:
#   • expecting silent numeric widening between i32 and i64 — use explicit ↦ conversion instead
#
# See also: tensor, type, conversion
# =============================================================================



# tensor arithmetic reject cases — expected compile failure
#
# WHY: documents the typecheck contract for elementwise tensor arithmetic.
# Both cases must reject:
#   1. tensor<string, …> — non-numeric element (numeric gate).
#   2. i32 + i64 — mixed width is a ↦ conversion problem,
#      never silent promotion at the kernel.
#
# This exemplum intentionally fails to compile; it is registered in the
# per-backend EXPECTED_FAILURES lists.

main {
    let tensor<string, [2]> words_a ← empty
    let tensor<string, [2]> words_b ← empty
    let tensor<string, [2]> words_sum ← words_a + words_b
    print words_sum.length()

    let tensor<i32, [2]> i32_a ← empty
    let tensor<i64, [2]> i64_b ← empty
    let tensor<i32, [2]> mixed ← i32_a + i64_b
    print mixed.length()
}

Expected: compilation rejects this example.

radix/corpus/reference/reject/tensor/elementwise-division-reject.fab (supporting · reject)#

/ between two aggregates is not elementwise; ⊘ floors and ÷ is true division.

# =============================================================================
# tensor — `/` between two aggregates names neither division.
# =============================================================================
#
# What this teaches:
#   • Elementwise division has two spellings, and they differ on integers:
#     `⊘` floors, `÷` is true division.
#   • `/` between two aggregates picks neither, so it is refused by name and
#     the source must say which rounding it wants.
#   • `/` stays scalar: dividing a tensor by a number.
#
# See also: tensor, ⊘, ÷, glyph-elementwise
# =============================================================================

# `/` over two [2] tensors: the elementwise glyph is ⊘ or ÷.
main {
    let tf32[2] a ← [4.0, 9.0] ↦ tf32[2]
    let tf32[2] b ← [2.0, 3.0] ↦ tf32[2]
    let tf32[2] q ← a / b
    print q.flatten()
}

Expected: compilation rejects this example.

radix/corpus/reference/reject/tensor/formata-reject.fab (supporting · reject)#

Rejected route: a formata reshape whose target element count differs from the receiver.

# =============================================================================
# tensor — formata reshape with a mismatched element count is rejected
# =============================================================================
#
# What this teaches:
#   • A `formata` reshape is a reinterpretation of the same elements: the
#     target Figura's element count must equal the receiver's, checked at
#     compile time (SEM010 tensor_formata_element_count_mismatch).
#   • The checker refuses a mismatch rather than padding or truncating.
#
# Common mistakes:
#   • expecting `formata` to resize — reshaping [1,2,3,4,5,6] (six elements) to
#     4x2 (eight elements) has no defined fill, so it is a compile error.
# =============================================================================

main {
    let list<f32> l ← [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
    let tf32[2, 3] data ← l ↦ tf32[2, 3]

    # 8 elements requested from a 6-element receiver: rejected.
    let tf32[4, 2] view ← data.formata<tf32[4, 2]>()
    print view.flatten()
}

Expected: compilation rejects this example.

radix/corpus/reference/reject/tensor/glyph-cross-width-reject.fab (supporting · reject)#

Glyph × is the 3-space cross product: both operands must carry width 3.

# =============================================================================
# × — cross width must be 3 on both sides.
# =============================================================================
#
# What this teaches:
#   • `×` is the cross product of 3-space, so both operands carry width 3.
#   • A width-4 operand has no cross product: the glyph is refused by name
#     rather than falling back to an outer or elementwise product.
#
# See also: tensor, ×, glyph-cross
# =============================================================================

# × with a width-4 operand: cross is width-3 on both sides.
main {
    let vf32[3] a ← [1.0, 0.0, 0.0] ↦ vf32[3]
    let vf32[4] b ← [0.0, 1.0, 0.0, 0.0] ↦ vf32[4]
    let vf32[4] c ← a × b
    print c
}

Expected: compilation rejects this example.

radix/corpus/reference/reject/tensor/glyph-hadamard-shape-reject.fab (supporting · reject)#

Glyph ⊙ requires identical operand shapes; it never stretches to broadcast.

# =============================================================================
# ⊙ — differing operand shapes are a named decline.
# =============================================================================
#
# What this teaches:
#   • `⊙` is pairwise elementwise: every lane of the left operand meets the
#     lane at the same index on the right.
#   • When the shapes differ there is no such lane, and no broadcast stretch is
#     implied. Align the shapes at the source instead.
#
# See also: tensor, ⊙, glyph-elementwise
# =============================================================================

# ⊙ over [2] and [3]: Hadamard has no broadcast stretch.
main {
    let tf32[2] a ← [1.0, 2.0] ↦ tf32[2]
    let tf32[3] b ← [1.0, 2.0, 3.0] ↦ tf32[3]
    let tf32[2] h ← a ⊙ b
    print h.flatten()
}

Expected: compilation rejects this example.

radix/corpus/reference/reject/tensor/glyph-scalar-operand-reject.fab (supporting · reject)#

Glyph ⊙ and ⊘ refuse a scalar operand: scale with * and / instead.

# =============================================================================
# ⊙ — a scalar operand is a named decline.
# =============================================================================
#
# What this teaches:
#   • `⊙` and `⊘` are elementwise operators over two shaped operands of the
#     same shape; a scalar has no shape, so the pair is refused by name.
#   • Scaling by a scalar keeps its own spelling: `*` and `/` on a tensor.
#
# See also: tensor, ⊙, ⊘, glyph-elementwise
# =============================================================================

# ⊙ with a scalar right operand: shaped-by-scalar is a named decline.
main {
    let list<f32> l ← [1.0, 2.0]
    let tf32[2] a ← l ↦ tf32[2]
    let tf32[2] scaled ← a ⊙ 2.0
    print scaled.flatten()
}

Expected: compilation rejects this example.

radix/corpus/reference/reject/tensor/glyph-transpose-rank1-reject.fab (supporting · reject)#

Postfix ᵀ refuses a rank-1 tensor: there is no row/column distinction to swap.

# =============================================================================
# ᵀ — rank-1 transpose is a named decline.
# =============================================================================
#
# What this teaches:
#   • `ᵀ` swaps the row and column axes of a rank-2 tensor only.
#   • A rank-1 tensor has one axis and no row/column distinction, so the
#     postfix refuses it by name instead of guessing an identity transpose.
#
# See also: tensor, ᵀ, glyph-transpose
# =============================================================================

# ᵀ on a rank-1 tensor: no row/column distinction to swap.
main {
    let tf32[3] a ← [1.0, 2.0, 3.0] ↦ tf32[3]
    let tf32[3] t ← aᵀ.materialize()
    print t.flatten()
}

Expected: compilation rejects this example.

radix/corpus/reference/reject/tensor/matrix-multiply-reject.fab (supporting · reject)#

* between two matrices is not the matrix product; · is.

# =============================================================================
# matrix — `*` is a scalar operator, not the matrix product.
# =============================================================================
#
# What this teaches:
#   • The matrix product is a glyph: `·` contracts the inner dimensions.
#   • `*` between two matrices is therefore a named decline, so the intent
#     (product or elementwise) is never guessed from the shapes.
#   • Elementwise pairing is `⊙`; `*` and `/` stay scalar scaling.
#
# See also: tensor, matrix, ·, ⊙
# =============================================================================

# `*` over two 2×2 matrices: the product glyph is `·`.
main {
    let matrix<f32, [2, 2]> a ← [[1.0, 2.0], [3.0, 4.0]] ↦ matrix<f32, [2, 2]>
    let matrix<f32, [2, 2]> b ← a * a
    print b
}

Expected: compilation rejects this example.

radix/corpus/reference/reject/tensor/tensor-multiply-reject.fab (supporting · reject)#

* between two tensors is not a product; ⊙ is the elementwise glyph.

# =============================================================================
# tensor — `*` is a scalar operator, not a tensor product.
# =============================================================================
#
# What this teaches:
#   • `*` between two tensors is a named decline, not a silent elementwise
#     multiply: the shape-pairing rule belongs to an explicit glyph.
#   • `⊙` is that glyph (elementwise), and `·` is the matrix product.
#   • `*` and `/` stay in their scalar role: scaling a tensor by a scalar.
#
# See also: tensor, ⊙, glyph-elementwise
# =============================================================================

# `*` over two [2] tensors: the elementwise glyph is ⊙.
main {
    let ti32[2] a ← [1, 2] ↦ ti32[2]
    let ti32[2] b ← a * a
    print b.flatten()
}

Expected: compilation rejects this example.

radix/corpus/reference/reject/tensor/vector-multiply-reject.fab (supporting · reject)#

* between two vectors is not elementwise; the glyphs say which product.

# =============================================================================
# vector — `*` is a scalar operator, not a vector product.
# =============================================================================
#
# What this teaches:
#   • A rank-1 aggregate has two products with different answers: the
#     elementwise `⊙` and the dot `·`.
#   • `*` names neither, so it is refused by name instead of defaulting to
#     one of them.
#   • `*` and `/` stay scalar: scaling a vector by a number.
#
# See also: tensor, vector, ⊙, ·
# =============================================================================

# `*` over two [2] vectors: the product glyph is ⊙ or `·`.
main {
    let vf32[2] v ← [1.0, 2.0] ↦ vf32[2]
    let vf32[2] w ← v * v
    print w
}

Expected: compilation rejects this example.