渲染zh-Hant

內核

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

Sugar @ 內核 fragment modifier marks a fragment-shader entry on the 內核 surface.

Syntax: @ 內核 片段

Category#

lane

Examples#

radix/corpus/annotation-sugar/nucleum-fragment.fab (canonical · annotation)#

Sugar @ 內核 fragment modifier marks a fragment-shader entry on the 內核 surface.

# =============================================================================
# 內核 — sugar @ 內核 片段 modifier
# =============================================================================
#
# What this teaches:
#   • Sugar modifier — `@ 內核 片段` on a function declaration marks
#     it as the fragment-shader entry (NucleumRole::Fragment); the parser
#     reads `片段` as structured modifier data on the 內核 annotation,
#     never as an untyped name/argument pair
#   • The la reader spelling — `內核` is the la surface of the kernel
#     annotation identity; the en twin is `@ kernel 片段`
#   • A marked 片段 function is a systems/shaders entry, not a host
#     entry — the plain `入口` driver below proves the module still
#     checks and runs on the host 車道
#
# Common mistakes:
#   • writing `@ 片段` alone — the bare 片段 name stays the graphics
#     statement annotation, not the 內核 modifier
#   • expecting the marked function to be called from a host `入口`
#
# See also: 內核, annotation-sugar
# =============================================================================

@ 內核 { 片段 = 真 }
函式 umbra() → 空值 {
}

函式 identitas(整數 x) → 整數 {
    傳回 x
}

入口 {
    註記 identitas(5)
}

Expected output:

5

radix/corpus/kernel-closure/argument-escape.fab (supporting · reject)#

A kernel closure cannot be passed to an ordinary function argument.

# =============================================================================
# 內核 — reject ordinary-argument escape
# =============================================================================

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

fn take() → void {
    const _ kernel ← (int value) kernel ∴ value
    const int result ← apply(kernel)
}

Expected: compilation rejects this example.

radix/corpus/kernel-closure/bag-result.fab (supporting · reject)#

A kernel closure cannot return a host list/bag, including an append-built result.

# =============================================================================
# 內核 — reject list/bag result protocol
# =============================================================================
# A list is a host-managed bag/result carrier. The closure cannot return it,
# even when it is populated with append before the return.
# =============================================================================

fn take(int value) → void {
    const _ bad ← (int value) kernel → list<int> ∴ do {
        var list<int> items ← empty
        items.append(value)
        return items
    }
}

Expected: compilation rejects this example.

radix/corpus/kernel-closure/bare-body.fab (supporting · reject)#

A compact closure body cannot be a bare block; use do in the body joint.

# =============================================================================
# 內核 — reject bare compact brace body
# =============================================================================

main {
    const _ bad ← (int value) kernel ∴ {
        return value
    }
}

Expected: compilation rejects this example.

radix/corpus/kernel-closure/cape.fab (supporting · reject)#

A kernel closure cannot contain do/catch recovery.

# =============================================================================
# 內核 — reject 捕捉/catch recovery in closure body
# =============================================================================

fn take(int value) → void {
    const _ bad ← (int value) kernel → int ∴ do {
        do {
            const int probe ← value
        } catch err {
            print err
        }
        return value
    }
}

Expected: compilation rejects this example.

radix/corpus/kernel-closure/capture.fab (supporting · reject)#

A kernel closure cannot capture an enclosing value local.

# =============================================================================
# 內核 — reject enclosing local capture
# =============================================================================

main {
    const int seed ← 2
    const _ bad ← (int value) kernel ∴ value + seed
}

Expected: compilation rejects this example.

radix/corpus/kernel-closure/dynamic-value.fab (supporting · reject)#

A kernel closure cannot produce a nested dynamic closure value.

# =============================================================================
# 內核 — reject dynamic closure value
# =============================================================================

fn take(int value) → void {
    const _ bad ← (int value) kernel ∴ (int inner) ∴ inner
}

Expected: compilation rejects this example.

radix/corpus/kernel-closure/error-channel.fab (supporting · reject)#

A kernel closure cannot declare an alternate-exit error channel.

# =============================================================================
# 內核 — reject closure error channel
# =============================================================================

main {
    const _ bad ← (int value) kernel → int ⇥ string ∴ value
}

Expected: compilation rejects this example.

radix/corpus/kernel-closure/field-escape.fab (supporting · reject)#

A kernel closure cannot be stored in a field.

# =============================================================================
# 內核 — reject field escape
# =============================================================================

class Capsule {
    var int stored
}

fn take() → void {
    const _ kernel ← (int value) kernel ∴ value
    const Capsule boxed ← Capsule { stored = kernel }
}

Expected: compilation rejects this example.

radix/corpus/kernel-closure/host-effect.fab (supporting · reject)#

A kernel closure cannot perform host output.

# =============================================================================
# 內核 — reject host effect in closure body
# =============================================================================

fn take(int value) → void {
    const _ bad ← (int value) kernel → int ∴ do {
        print value
        return value
    }
}

Expected: compilation rejects this example.

radix/corpus/kernel-closure/list-escape.fab (supporting · reject)#

A kernel closure cannot be stored in a list.

# =============================================================================
# 內核 — reject list escape
# =============================================================================

fn take() → void {
    const _ kernel ← (int value) kernel ∴ value
    const list<int> items ← [kernel, kernel]
}

Expected: compilation rejects this example.

radix/corpus/kernel-closure/reject.fab (supporting · reject)#

A kernel closure cannot use a reject guard that throws.

# =============================================================================
# 內核 — reject reject guard in closure body
# =============================================================================

fn take(int value) → void {
    const _ bad ← (int value) kernel → int ∴ do {
        reject value > 10 throw value
        return value
    }
}

Expected: compilation rejects this example.

radix/corpus/kernel-closure/require.fab (supporting · reject)#

A kernel closure cannot use a require guard that throws.

# =============================================================================
# 內核 — reject require guard in closure body
# =============================================================================

fn take(int value) → void {
    const _ bad ← (int value) kernel → int ∴ do {
        require value < 10 throw value
        return value
    }
}

Expected: compilation rejects this example.

radix/corpus/kernel-closure/return-escape.fab (supporting · reject)#

A kernel closure cannot be returned as an outer function value.

# =============================================================================
# 內核 — reject return escape
# =============================================================================

fn take() → void {
    const _ kernel ← (int value) kernel ∴ value
    return kernel
}

Expected: compilation rejects this example.

radix/corpus/kernel-closure/source-callback.fab (supporting · reject)#

A kernel closure cannot capture a host source callback value.

# =============================================================================
# 內核 — reject source callback capture
# =============================================================================
# A source callback is an enclosing function value. Referencing it from a
# kernel closure is a capture and therefore cannot cross the device boundary.
# =============================================================================

fn take((string, int) → int source, int value) → void {
    const _ bad ← (int x) kernel → int ∴ source("weights", x)
}

Expected: compilation rejects this example.

radix/corpus/kernel-closure/stack-free-kernel.fab (supporting · reject)#

A closure cannot stack free and kernel modifiers.

# =============================================================================
# 內核 — reject stacked free then kernel modifiers
# =============================================================================

main {
    const _ bad ← (int value) free kernel ∴ value
}

Expected: compilation rejects this example.

radix/corpus/kernel-closure/stack-kernel-free.fab (supporting · reject)#

A closure cannot stack kernel and free modifiers.

# =============================================================================
# 內核 — reject stacked kernel then free modifiers
# =============================================================================

main {
    const _ bad ← (int value) kernel free ∴ value
}

Expected: compilation rejects this example.

radix/corpus/kernel-closure/throw.fab (supporting · reject)#

A kernel closure cannot throw an alternate-exit value.

# =============================================================================
# 內核 — reject throw in closure body
# =============================================================================

fn take(int value) → void {
    const _ bad ← (int value) kernel → int ∴ do {
        throw value
        return value
    }
}

Expected: compilation rejects this example.

radix/corpus/kernel/legality-reject.fab (supporting · reject)#

Kernel legality at check time: host output, host-only types, calls to non-kernels, host calls to private kernels, and raw thread-id calls are named diagnostics.

# =============================================================================
# 內核 — kernel legality is decided by `faber check`
# =============================================================================
#
# What this teaches:
#   • A kernel body must not need the host runtime: no console output, no
#     conversations, no async or generator forms, no heap-backed values
#     (`list`, `map`, `set`, `string`, `JSON`, `value`), no local handlers.
#   • A kernel may call another kernel or a built-in device operation, never
#     an ordinary function.
#   • Only a public kernel is launchable: host code may call a public kernel
#     but not a private one.
#   • A kernel never names its own 車道: the thread-id builtins
#     (`global_invocation_id()` and its siblings) are raw 車道 ids and are
#     rejected in user code; the parallel pattern is an `遍歷 … 在`
#     loop over the 張量.
#   • Each violation is a named diagnostic on the offending line, reported
#     by `faber check` before any device lowering runs.
#
# Common mistakes:
#   • printing from a kernel to debug it — run the kernel on the runner
#     oracle instead.
#   • passing a `list` to a kernel — use a 張量.
#
# See also: 內核, 公開
# =============================================================================

fn host_double(f32 x) → f32 {
    return x * 2.0
}

# 1. host output
@ kernel
@ public
fn noisy(f32 x) → f32 {
    print x
    return x
}

# 2. call to an ordinary function
@ kernel
@ public
fn calls_host(f32 x) → f32 {
    return host_double(x)
}

# 3. host-only parameter type
@ kernel
@ public
fn takes_list(list<f32> xs) → void {
}

# 4. host call to a private kernel
@ kernel
fn helper(f32 x) → f32 {
    return x + 1.0
}

fn launches_private() → f32 {
    return helper(1.0)
}

# 5. raw 車道 id
@ kernel
@ public
fn names_its_lane(f32 x) → f32 {
    const 向量<u32, 3> gid ← global_invocation_id()
    return x
}

Expected: compilation rejects this example.